package io.wins.vertx.verticle.api;

import io.vertx.core.Future;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.wins.vertx.entity.Customer;
import io.wins.vertx.entity.CustomerAddress;
import io.wins.vertx.service.CustomerService;
import org.apache.commons.lang3.StringUtils;

public class CustomerApiVerticle extends RestApiVerticle {

    public static final String REST_ADDRESS = "io.wins.rest.customer#%1$s";
    public static final String ADDRESS_LIST = "addressCreate";
    public static final String ADDRESS_SAVE = "addressSave";
    public static final String ADDRESS_DELETE = "addressDelete";
    private static final Logger logger = LoggerFactory.getLogger(CustomerApiVerticle.class);

    public final CustomerService service;

    public CustomerApiVerticle(final CustomerService customerService) {
        this.service = customerService;
    }

    @Override
    public void start(final Future<Void> startFuture) throws Exception {
        super.start();
        this.on(String.format(REST_ADDRESS, LOAD), this::loadCustomer);
        this.on(String.format(REST_ADDRESS, LIST), this::listCustomer);
        this.on(String.format(REST_ADDRESS, UPDATE), this::updateCustomer);
        this.on(String.format(REST_ADDRESS, DELETE), this::deleteCustomer);
        this.on(String.format(REST_ADDRESS, ADDRESS_DELETE), this::deleteAddress);
        this.on(String.format(REST_ADDRESS, ADDRESS_LIST), this::listAddress);
        this.on(String.format(REST_ADDRESS, ADDRESS_SAVE), this::saveAddress);
        this.on(String.format(REST_ADDRESS, ADDRESS_DELETE), this::deleteAddress);
        startFuture.complete();
    }

    private void loadCustomer(final Message<JsonObject> tMessage) {
        final String id = tMessage.body().getJsonObject("param").getString("id");
        this.service.loadCustomer(id, tMessage);
    }

    private void deleteCustomer(final Message<JsonObject> tMessage) {
        final String id = tMessage.body().getJsonObject("param").getString("id");
        this.service.delCustomer(id, tMessage);
    }

    private void updateCustomer(final Message<JsonObject> tMessage) {
        final JsonObject body = tMessage.body();
        final JsonObject cust = body.getJsonObject("body").getJsonObject("customer");
        final Customer customer = new Customer(cust);
        this.service.updateCustomer(customer, tMessage);
    }

    private void listCustomer(final Message<JsonObject> tMessage) {
        this.service.listCustomer(tMessage);
    }

    private void deleteAddress(final Message<JsonObject> tMessage) {
        final String id = tMessage.body().getJsonObject("param").getString("id");
        this.service.delCustAddress(id, tMessage);
    }

    private void saveAddress(final Message<JsonObject> tMessage) {
        final JsonObject body = tMessage.body();
        final String id = body.getJsonObject("param").getString("id");
        final JsonObject addr = body.getJsonObject("body").getJsonObject("address");
        final CustomerAddress address = new CustomerAddress(addr);
        if (StringUtils.isEmpty(id)) {
            this.service.createCustAddress(address, tMessage);
        } else {
            this.service.saveCustAddress(address, tMessage);
        }
    }

    private void listAddress(final Message<JsonObject> tMessage) {
        final String cid = tMessage.body().getJsonObject("param").getString("cid");
        this.service.listCustAddress(cid, tMessage);
    }
}
